import traceback
from flask import Flask, jsonify, send_from_directory, request
from analyze import all_in_one

app = Flask(__name__)
@app.route("/analyze/<stock_code>", methods=['POST'])
def analyze(stock_code):
    # default_auth_key = "b585f727c42d4b058eb559fc1f286b00.WWV4QeWiZprlw7yR0tgpqABM"
    auth_key = request.headers.get("auth_key")
    # param = jsonify(request.get_data().decode('utf-8'))
    # 从request body中获取json格式的参数转换为AnalyzeParam对象
    param = request.get_json()
    print(type(param))  
    try:
        ret = all_in_one(stock_code, auth_key, param['period'], param['adjust'])
        return stock_code + '分析结果\n' + ret
    except Exception as e:
        traceback.print_exc()
        return '分析过程出现错误' + str(e)
    # return 'stock_code + 分析结果\n' + all_in_one(stock_code, auth_key)


@app.route("/<path:filename>")
def index(filename):
    return send_from_directory('app', filename)

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False)

class AnalyzeParam:
    def __init__(self, period='daily', adjust='qfq'):
        self.period = period
        self.adjust = adjust